package com.yoga.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoga.model.Course;
import com.yoga.service.CourseService;
import com.yoga.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yan
 * @date 2025/8/16
 */
@RestController
@RequestMapping(value = "/api/v1/course", produces = MediaType.APPLICATION_JSON_VALUE)
public class CourseApi {

    private CourseService courseService;
    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    //查询
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                                 @RequestParam(defaultValue = "10") Integer pageSize,Course course){

        //System.out.println(course);
        Page<Course> page = new Page<>(pageNo,pageSize);
        page = this.courseService.findAll(page,course);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @GetMapping("/all")
    public ResponseEntity<JsonResult<?>> findAlls(){
        return ResponseEntity.ok(JsonResult.success(this.courseService.findAlls()));
    }

    //删除
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds( @RequestBody Integer[] ids){
        int count = this.courseService.deleteByIds(List.of(ids));
        return ResponseEntity.ok(JsonResult.success(count));
    }

    //添加
    @PostMapping
    public ResponseEntity<JsonResult<?>> add(@RequestBody Course course){
        boolean flag = this.courseService.add(course);
        if(flag){
            return ResponseEntity.ok(JsonResult.success(course));
        }else {
            return ResponseEntity.ok(JsonResult.fail("添加失败"));
        }
    }

    //修改
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody Course course){
        //System.out.println( course);
        boolean flag = this.courseService.update(course);
        if(flag){
            return ResponseEntity.ok(JsonResult.success(course));
        }else {
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }

}
